
#include "msg_review.h"
#include "sys_msg.h"
#include "sys_font.h"
#include "pt_fs.h"

static ui_msg_review_t* m_re  = NULL;
static dev_info_t*      m_dev = NULL;

static char* msg_return[1] = 
{
    "Return.png",
};

static int msg_review_esc_clicked(lv_event_t* event)
{
    page_pop();

    return 0;
}

static void msg_review_esc_event_callback(lv_event_t* event)
{
    if (event->code == LV_EVENT_CLICKED) {
        debug_d("esc\n");
        msg_review_esc_clicked(event);
    }
}

int ui_msg_review_create(page_base_t* self)
{
    lv_coord_t pw = 0;
    lv_coord_t ph = 0;

    lv_obj_t* superview   = NULL;
    ui_msg_review_t* m_re = NULL;

    superview = self->root;
    m_re      = (ui_msg_review_t*)self->user_data;
    //**********************************************
    if(!m_re){
        return -1;
    }
    //**********************************************

    pw = lv_disp_get_hor_res(NULL);
    ph = lv_disp_get_ver_res(NULL);

    sys_msg_t * info    = NULL;
    info = msg_info_get_by_index(m_re->data.index);
    if(!info){
        return -1;
    }

    debug_d("w:h=%d:%d\n",pw,ph);
    debug_d("title=%s\n",info->e_attr.title);
    debug_d("txtbf=%s\n",info->e_attr.buff);

    m_re->ui.esc_bg = lv_obj_create(superview);

    lv_obj_set_pos(m_re->ui.esc_bg,0,0);
    lv_obj_set_size(m_re->ui.esc_bg,60,40);

	lv_obj_set_style_radius(m_re->ui.esc_bg, 0, LV_PART_MAIN);
	lv_obj_set_style_border_width(m_re->ui.esc_bg, 0, LV_PART_MAIN);
	lv_obj_set_style_border_color(m_re->ui.esc_bg, lv_color_make(0x00, 0x00, 0x00), LV_PART_MAIN);
	lv_obj_set_style_border_opa(m_re->ui.esc_bg, LV_OPA_0, LV_PART_MAIN);

	lv_obj_set_style_outline_width(m_re->ui.esc_bg, 0, LV_PART_MAIN);
	lv_obj_set_style_shadow_width(m_re->ui.esc_bg, 0, LV_PART_MAIN);
	lv_obj_set_scrollbar_mode(m_re->ui.esc_bg, LV_SCROLLBAR_MODE_OFF);
	lv_obj_set_style_pad_all(m_re->ui.esc_bg, 0,0);
    
    #if 1
    lv_obj_set_style_bg_opa(m_re->ui.esc_bg,LV_OPA_0,0);
    #else
    lv_obj_set_style_bg_opa(m_re->ui.esc_bg,LV_OPA_100,0);
    lv_obj_set_style_bg_color(m_re->ui.esc_bg,lv_color_make(0xff,0,0),0);
    #endif 
    lv_obj_add_event_cb(m_re->ui.esc_bg,msg_review_esc_event_callback,LV_EVENT_ALL,NULL);
    //************************
    m_re->ui.esc_img = lv_img_create(m_re->ui.esc_bg);
    lv_img_set_src(m_re->ui.esc_img,pt_fs_get(msg_return[0]));
    lv_obj_set_pos(m_re->ui.esc_img,5,5);
    //************************************************************************* */

    m_re->ui.lab_title = lv_label_create(superview);
    lv_obj_set_pos(m_re->ui.lab_title,90,60);
    lv_obj_set_size(m_re->ui.lab_title,300,40);
    lv_obj_set_style_text_align(m_re->ui.lab_title,LV_TEXT_ALIGN_CENTER,0);
    lv_obj_set_style_text_opa(m_re->ui.lab_title,LV_OPA_100,LV_PART_MAIN);
    lv_obj_set_style_text_color(m_re->ui.lab_title,lv_color_make(0xff,0xff,0xff),LV_PART_MAIN); 
    lv_obj_set_style_text_font(m_re->ui.lab_title, lv_font_get(24), LV_STATE_DEFAULT);
    lv_label_set_long_mode(m_re->ui.lab_title,LV_LABEL_LONG_SCROLL);

    lv_label_set_text(m_re->ui.lab_title,info->e_attr.title);

    #if 1
    lv_obj_set_style_bg_opa(m_re->ui.lab_title,LV_OPA_0,0);
    #else
    lv_obj_set_style_bg_opa(m_re->ui.lab_title,LV_OPA_100,0);
    lv_obj_set_style_bg_color(m_re->ui.lab_title,lv_color_make(0xff,0,0),0);
    #endif 
    //************************************************************************* */

    m_re->ui.lab_detail = lv_label_create(superview);
    lv_obj_set_pos(m_re->ui.lab_detail,0,110);
    lv_obj_set_size(m_re->ui.lab_detail,480,200);
    lv_obj_set_style_text_align(m_re->ui.lab_detail,LV_TEXT_ALIGN_CENTER,0);
    lv_obj_set_style_text_opa(m_re->ui.lab_detail,LV_OPA_40,LV_PART_MAIN);
    lv_obj_set_style_text_color(m_re->ui.lab_detail,lv_color_make(0xff,0xff,0xff),LV_PART_MAIN); 
    lv_obj_set_style_text_font(m_re->ui.lab_detail, lv_font_get(24), LV_STATE_DEFAULT);

    lv_label_set_text(m_re->ui.lab_detail,info->e_attr.buff);

    #if 1
    lv_obj_set_style_bg_opa(m_re->ui.lab_detail,LV_OPA_0,0);
    #else
    lv_obj_set_style_bg_opa(m_re->ui.lab_detail,LV_OPA_50,0);
    lv_obj_set_style_bg_color(m_re->ui.lab_detail,lv_color_make(0x99,0x99,0x99),0);
    #endif 

    lv_obj_set_style_radius(m_re->ui.lab_detail, 5, LV_PART_MAIN);
	lv_obj_set_style_border_width(m_re->ui.lab_detail, 0, LV_PART_MAIN);
	lv_obj_set_style_border_color(m_re->ui.lab_detail, lv_color_make(0x00, 0x00, 0x00), LV_PART_MAIN);
	lv_obj_set_style_border_opa(m_re->ui.lab_detail, LV_OPA_0, LV_PART_MAIN);

	lv_obj_set_style_outline_width(m_re->ui.lab_detail, 0, LV_PART_MAIN);
	lv_obj_set_style_shadow_width(m_re->ui.lab_detail, 0, LV_PART_MAIN);

    return 0;
}


int ui_msg_review_destroy(page_base_t* self)
{
    lv_obj_t* superview   = NULL;
    ui_msg_review_t* m_re = NULL;

    superview = self->root;
    m_re      = (ui_msg_review_t*)self->user_data;
    //**********************************************
    if(!m_re){
        return -1;
    }
    //**********************************************
    if(m_re->ui.esc_bg){
        lv_obj_del(m_re->ui.esc_bg);
        m_re->ui.esc_bg = NULL;
        m_re->ui.esc_img = NULL;
    }
    if(m_re->ui.lab_title){
        lv_obj_del(m_re->ui.lab_title);
        m_re->ui.lab_title = NULL;
    }
    if(m_re->ui.lab_detail){
        lv_obj_del(m_re->ui.lab_detail);
        m_re->ui.lab_detail = NULL;
    }

    return 0;
}

static void msg_review_on_view_did_load(page_base_t* self)
{
    ui_msg_review_create(self);
}
static void msg_review_on_custom_attr_config(page_base_t* self)
{

}
static void msg_review_on_view_load(page_base_t* self)
{

}
static void msg_review_on_view_will_appear(page_base_t* self)
{

}
static void msg_review_on_view_did_appear(page_base_t* self)
{

}
static void msg_review_on_view_will_disappear(page_base_t* self)
{

}
static void msg_review_on_view_did_disappear(page_base_t* self)
{

}
static void msg_review_on_view_did_unload(page_base_t* self)
{
    ui_msg_review_destroy(self);
}

int ui_msg_review_init(void)
{
    m_re = (ui_msg_review_t*)psram_malloc(sizeof(ui_msg_review_t));
    if(!m_re){
        return -1;
    }

    memset(m_re,0,sizeof(ui_msg_review_t));
    //*********************************************
    m_dev = (dev_info_t*)psram_malloc(sizeof(dev_info_t));
    if(!m_dev){
        debug_d("merr\n");
        psram_free(m_re);
        return NULL;
    }
    memset(m_dev,0,sizeof(dev_info_t));
    //*********************************************
    page_vtable_t*   method  = NULL;
    method = (page_vtable_t*)malloc(sizeof(page_vtable_t));
    if(!method){
        debug_d("merr\n");
        return NULL;
    }
    method->on_custom_attr_config  = msg_review_on_custom_attr_config;
    method->on_view_load           = msg_review_on_view_load;
    method->on_view_did_load       = msg_review_on_view_did_load;
    method->on_view_will_appear    = msg_review_on_view_will_appear;
    method->on_view_did_appear     = msg_review_on_view_did_appear;
    method->on_view_will_disappear = msg_review_on_view_will_disappear;
    method->on_view_did_disappear  = msg_review_on_view_did_disappear;
    method->on_view_did_unload     = msg_review_on_view_did_unload;
    //*********************************************
    m_dev->method     = method;    
    m_dev->priv_data  = (void*)m_re;
    //*********************************************

    return 0;
}

dev_info_t* ui_msg_review_get_handle(void)
{
    return m_dev;
}
